<?php

namespace app\api\controller\admin;

use app\common\model\ThridApp as ThridAppModel;
use app\common\model\AdminUser as AdminUserModel;
use think\Controller;
use think\Request;

/**
 * 登录接口
 * Class Login
 * @package app\api\controller
 */
class Login extends Controller
{
    public function login()
    {
        $request = Request::instance();
        $param = $request->param();

        $map = [];
        $map['app_id'] = $param['app_id'];
        $thrid_app_model = new ThridAppModel();
        $thrid_app = $thrid_app_model->where($map)->find();
        if (!$thrid_app) {
            $data['code'] = 401;
            $data['msg'] = '非法操作';
            $data['error_code'] = 10001;
            return json($data);
        }

        if ($thrid_app->delete_time > 0) {
            $data['code'] = 401;
            $data['msg'] = '非法操作';
            $data['error_code'] = 10001;
            return json($data);
        }

        $app_secret = md5($param['app_id'] . $thrid_app->app_secret . $param['time']);
        if ($app_secret != $param['app_secret_des']) {
            $data['code'] = 401;
            $data['msg'] = '第三方秘钥错误';
            $data['error_code'] = 10002;
            return json($data);
        }

        $map = [];
        $map['username'] = $param['username'];
        $map['password'] = $param['password'];
        $admin_user_model = new AdminUserModel();
        $user = $admin_user_model->where($map)->find();

        if (!empty($user)) {
            if ($user['status'] != 1) {
                $data['code'] = 401;
                $data['msg'] = '当前用户已禁用';
                $data['error_code'] = 10003;
                return json($data);
            } else {
                $map = [];
                $map['id'] = $user['id'];
                $admin_user_model->save(
                    [
                        'last_login_time' => date('Y-m-d H:i:s', time()),
                        'last_login_ip' => $this->request->ip()
                    ], $map
                );

                $token_model = new Token();
                $token = $token_model->getToken($user['id'], $thrid_app->app_id, $thrid_app->app_secret);
                if ($token) {
                    $data['code'] = 200;
                    $data['msg'] = '登录成功';
                    $data['url'] = '/admin/index/index';
                    $data['admin_id'] = $user->id;
                    $data['admin_name'] = $user->username;
                    $data['admin_token'] = $token['token'];
                    $data['admin_time_out'] = $token['time_out'];
                    $data['error_code'] = 0;
                    return json($data);
                } else {
                    $data['code'] = 401;
                    $data['msg'] = 'Token获取失败';
                    $data['error_code'] = 10004;
                    return json($data);
                }
            }
        } else {
            $data['code'] = 401;
            $data['msg'] = '用户名或密码错误';
            $data['error_code'] = 10005;
            return json($data);
        }
    }


}